package com.test.getorload;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;

import com.pojo.Product;

/**
 * @description get方式获取
 * @author Leon Plious
 * @date 2020年7月6日
 */
public class HibernateTestGetAndLoad {

	/*
log1
Hibernate: select product0_.id as id0_0_, product0_.name as name0_0_, product0_.price as price0_0_, product0_.cid as cid0_0_ from product_ product0_ where product0_.id=?
Hibernate: select users0_.pid as pid0_1_, users0_.uid as uid1_, user1_.id as id3_0_ from user_product users0_ inner join user_ user1_ on users0_.uid=user1_.id where users0_.pid=?
log2
log3
	 */
	
	public static void main(String[] args) {
		 SessionFactory sf = new Configuration().configure().buildSessionFactory();
		 Session session = sf.openSession();
		 session.beginTransaction();
		 
		 
		 System.out.println("log1");
		 // get方式加载，加载即访问
		 Product p1 = (Product) session.get(Product.class, 1);
		 System.out.println("log2");
		// load方式延迟加载，只要属性访问的时候才会访问数据库
		 Product p2 = (Product) session.load(Product.class, 2);
		 System.out.println("log3");
		 
		 session.getTransaction().commit();
		 session.close();
		 sf.close();
	}
}
